package com.coolingme.Reader;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileReaderTest {

    public static void main(String[] args) {
        File file = new File("D://Java//IntelliJIDEA//workspace//java-learn//IO//file//fileReader//reader.txt");
        FileReader in = null;
        FileWriter out = null;
        try {
            in = new FileReader(file);
            out = new FileWriter("D://Java//IntelliJIDEA//workspace//java-learn//IO//file//fileReader//copy//reader.txt");

            int code = in.read();// 读取单个
            System.out.println(code);
            System.out.println((char) code);

            char[] c = new char[1024];//小推车
            int count = in.read(c);
            while (count != -1) {//循环读取
                System.out.println(count);
                System.out.println(new String(c, 0, count));//读的数据
                out.write(c, 0, count);//写
                out.flush();//刷新
                count = in.read();//再读
            }
            out.write(97);//写入 字符
            out.write("大家好！");//写入 字符串
            System.out.println(file.getName() + "读写完成！");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
